package com.ctyon.watchmanager.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.launcher.ARouter;
import com.ctyon.common.event.AddWatch;
import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.databinding.FragmentAddWatchBinding;
import com.ctyon.watchmanager.ui.BaseFragment;
import com.ctyon.watchmanager.present.AddPresenter;
import com.orhanobut.logger.Logger;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import static com.ctyon.common.container.ARouterPath.ROUTER_ACTIVITY_CONTAINER;
import static com.ctyon.watchmanager.ui.activity.ContainerActivity.PARAM1;
import static com.ctyon.watchmanager.ui.activity.ContainerActivity.TAB_INDEX_PARAM;

/**
 * CreateDate：19-6-25 on 上午9:14
 * Describe:
 * Coder: lee
 */
public class AddWatchFragment extends BaseFragment {

    public static final String TAG = "AddWatchFragment";
    private static final int REQUEST_CODE_SCAN = 111;

    private FragmentAddWatchBinding mBinding;

    public static AddWatchFragment newInstance() {
        final AddWatchFragment fragment = new AddWatchFragment();
        final Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);

        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_add_watch, container, false);
        mBinding.setAddPresenter(addPresenter);

        return mBinding.getRoot();

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private final AddPresenter addPresenter = new AddPresenter() {
        @Override
        public void onQRCodeClick() {
            Intent intent = new Intent(getActivity(), CaptureActivity.class);
            /*ZxingConfig是配置类
             *可以设置是否显示底部布局，闪光灯，相册，
             * 是否播放提示音  震动
             * 设置扫描框颜色等
             * 也可以不传这个参数
             * */
            ZxingConfig config = new ZxingConfig();
            config.setShowAlbum(false);
            config.setShowFlashLight(false);
            config.setShowbottomLayout(false);
            //config.setPlayBeep(false);//是否播放扫描声音 默认为true
            //config.setShake(false);//是否震动  默认为true
            //config.setDecodeBarCode(false);//是否扫描条形码 默认为true
            //config.setReactColor(R.color.colorAccent);//设置扫描框四个角的颜色 默认为白色
            //config.setFrameLineColor(R.color.colorAccent);//设置扫描框边框颜色 默认无色
            //config.setScanLineColor(R.color.colorAccent);//设置扫描线的颜色 默认白色
            config.setFullScreenScan(false);//是否全屏扫描  默认为true  设为false则只会在扫描框中扫描
            intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
            startActivityForResult(intent, REQUEST_CODE_SCAN);
        }

        @Override
        public void onInputClick() {
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, AddWatchInputFragment.TAG)
                    .navigation();
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.t(LTAG).d("onDestroy");
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void onAddWatch(AddWatch addWatch){
        getActivity().finish();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // 扫描二维码/条码回传
        if (requestCode == REQUEST_CODE_SCAN && resultCode == getActivity().RESULT_OK) {
            if (data != null) {
                String content = data.getStringExtra(Constant.CODED_CONTENT);
                Logger.t(LTAG).d("scan result : " + content);
                ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                        .withString(TAB_INDEX_PARAM, AddWatchInputFragment.TAG)
                        .withString(PARAM1, content)
                        .navigation();
            }
        }
    }
}
